package com.terrence.sso.token.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public final class URLUtils {
	
	private URLUtils(){};
	
	/**
	 * 获取除参数外的URL
	 * @param url
	 * @return
	 */
	public static String getServer(String url){
		if(0 <= url.indexOf("?")){
			return url;
		}else{
			return url.substring(0, url.indexOf("?"));
		}
	}
	/**
	 * 添加参数
	 * @param url
	 * @param params
	 * @return
	 */
	public static String appendParams(String url,HashMap<String,String> params){
		StringBuilder newUrl = new StringBuilder();
		newUrl.append(url);
		if (0 <= url.indexOf("?")) {
            newUrl.append("&");
        } else {
        	newUrl.append("?");
        }
		if(null!=params && !params.isEmpty()){
			ArrayList<String> parts = new ArrayList<String>();
			Iterator<String> itor = params.keySet().iterator();
			while(itor.hasNext()){
				String key = itor.next();
				String value = params.get(key);
				parts.add(key+"="+value);
			}
			newUrl.append(StringUtils.joins(parts, "&"));
		}
		System.out.println(newUrl.toString());
		return newUrl.toString();
	}
}
